package com.oba.fusioncharts.utils;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;

import com.oba.fusioncharts.data.LineDataSet;
import com.oba.fusioncharts.data.LineCondition;
import com.oba.fusioncharts.data.PieCondition;
import com.oba.fusioncharts.data.PieData;

public class FusionUtil {
	
	public static String generatePieData(PieCondition condition,List<PieData> pieDataList) {
		DocumentFactory factory = DocumentFactory.getInstance();
		Document doc = factory.createDocument();
		doc.setXMLEncoding("UTF-8");
		Element rootElement = doc.addElement("chart");
		rootElement.addAttribute("caption", condition.getCaption());
		rootElement.addAttribute("subcaption", condition.getSubcaption());
		rootElement.addAttribute("formatNumberScale", condition
				.getFormatNumberScale().toString());
		for (PieData data:pieDataList) {
			Element set = rootElement.addElement("set");
			set.addAttribute("label", data.getLabel());
			set.addAttribute("value", data.getValue());
			if (data.getIsSliced() == 1) {
				set.addAttribute("isSliced", "1");
			}
		}
		return doc.asXML();
	}

	public static String generateLineData(LineCondition condition,
			List<String> categoryList, List<LineDataSet> datasetList) {
		DocumentFactory factory = DocumentFactory.getInstance();
		Document doc = factory.createDocument();
		doc.setXMLEncoding("UTF-8");
		Element rootElement = doc.addElement("chart");
		rootElement.addAttribute("caption", condition.getCaption());
		rootElement.addAttribute("subcaption", condition.getSubcaption());
		rootElement.addAttribute("lineThickness", condition.getLineThickness()
				.toString());
		rootElement.addAttribute("showValues", condition.getShowValues()
				.toString());
		rootElement.addAttribute("formatNumberScale", condition
				.getFormatNumberScale().toString());
		rootElement.addAttribute("anchorRadius", condition.getAnchorRadius()
				.toString());
		rootElement.addAttribute("divLineAlpha", condition.getDivLineAlpha()
				.toString());
		rootElement.addAttribute("divLineColor", condition.getDivLineColor());
		rootElement.addAttribute("divLineIsDashed", condition
				.getDivLineIsDashed().toString());
		rootElement.addAttribute("showAlternateHGridColor", condition
				.getShowAlternateHGridColor().toString());
		rootElement.addAttribute("alternateHGridColor", condition
				.getAlternateHGridColor());
		rootElement.addAttribute("shadowAlpha", condition.getShadowAlpha()
				.toString());
		rootElement.addAttribute("labelStep", condition.getLabelStep()
				.toString());
		rootElement.addAttribute("numvdivlines", condition.getNumvdivlines()
				.toString());
		rootElement.addAttribute("chartRightMargin", condition
				.getChartRightMargin().toString());
		rootElement.addAttribute("bgColor", condition.getBgColor());
		rootElement.addAttribute("bgAngle", condition.getBgAngle().toString());
		rootElement.addAttribute("bgAlpha", condition.getBgAlpha());
		rootElement.addAttribute("alternateHGridAlpha", condition
				.getAlternateHGridAlpha().toString());
		rootElement.addAttribute("legendPosition", condition
				.getLegendPosition());

		Element categories = rootElement.addElement("categories");
		for (String label : categoryList) {
			categories.addElement("category").addAttribute("label", label);
		}

		for (LineDataSet dataSet : datasetList) {
			Element dsElement = rootElement.addElement("dataset");
			dsElement.addAttribute("seriesName", dataSet.getSeriesName());
			dsElement.addAttribute("color", dataSet.getColor());
			dsElement.addAttribute("anchorBorderColor", dataSet
					.getAnchorBorderColor());
			dsElement.addAttribute("anchorBgColor", dataSet.getAnchorBgColor());
			for (String value : dataSet.getValueSet()) {
				dsElement.addElement("set").addAttribute("value", value);
			}
		}

		// styles
		Element styles = rootElement.addElement("styles");
		Element definition = styles.addElement("definition");
		Element style = definition.addElement("style");
		style.addAttribute("name", "CaptionFont");
		style.addAttribute("type", "font");
		style.addAttribute("size", "12");
		Element application = styles.addElement("application");
		application.addElement("apply").addAttribute("toObject", "CAPTION")
				.addAttribute("styles", "CaptionFont");
		application.addElement("apply").addAttribute("toObject", "SUBCAPTION")
				.addAttribute("styles", "CaptionFont");

		return doc.asXML();
	}
}
